<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import { debounce } from "@/utils";

export default {
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "240px",
    },
    chartData: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        // this.initChart();
        this.setOptions(val);
      },
    },
  },
  mounted() {
    // this.$nextTick(() => {
    //   this.initChart()
    // })

    this.initChart();
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize();
      }
    }, 100);
    window.addEventListener("resize", this.__resizeHandler);
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions({ date, data } = {}) {
      this.chart.setOption({
        grid: {
          top: "15%",
          left: "4%",
          right: "6%",
          bottom: "3%",
          containLabel: true,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
          },
          padding: [5, 10],
        },
        xAxis: {
          data: date,
          boundaryGap: false,
          axisTick: {
            show: false,
          },
          axisLine: {
            lineStyle: {
              color: "#9D9D9D", //左边线的颜色
            },
          },
          axisLabel: {
            textStyle: {
              color: "#9D9D9D", //坐标值得具体的颜色
            },
          },
        },
        yAxis: {
          // name: "单位：元",
          axisTick: {
            show: false,
          },
          splitArea: {
            show: true,
            areaStyle: {
              color: ["#fff"],
            },
          },
          axisLine: {
            lineStyle: {
              color: "#9D9D9D", //左边线的颜色
            },
          },
          axisLabel: {
            textStyle: {
              color: "#9D9D9D", //坐标值得具体的颜色
            },
          },
        },
        series: [
          {
            smooth: true,
            type: "line",
            // symbol:"rect",
            symbolSize: 5, //拐点大小
            itemStyle: {
              normal: {
                color: "#3888fa",
                lineStyle: {
                  color: "#3888fa",
                  width: 3,
                },
                areaStyle: {
                  color: new echarts.graphic.LinearGradient(
                    0,
                    0,
                    0,
                    1, //变化度
                    //三种由深及浅的颜色
                    [
                      {
                        offset: 0,
                        color: "#A5CAFA",
                      },
                      {
                        offset: 0.5,
                        color: "#F1F7FE",
                      },
                      {
                        offset: 1,
                        color: "#F3F8FF",
                      },
                    ]
                  ),
                },
              },
            },
            data: data,
            // animationDuration: 2800,
            // animationEasing: "quadraticOut",
          },
        ],
      });
    },
  },
};
</script>
